"""
vis3d/color.py
Color handling class and some simple color definitions.

Written By:
    James Thomas
    Email: jim@houseoftechnology.org
    Web: http://mission-cognition.houseoftechnology.org/

Copyright 2009-2012

This file is part of the PandaODElib distribution.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version. The text of the
GNU Lesser General Public License is included with this library in
the file named LICENSE.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""

import colorsys
from pandac.PandaModules import VBase4

class Color(object):
    def __init__(self, *args):
        if len(args) == 1:
            args = args[0]
        if len(args) == 4:
            red, green, blue, alpha = args
        else:
            red, green, blue = args
            alpha = 1
            
        self._values = VBase4(red, green, blue, alpha)

    def __len__(self):
        return 4

    def __iter__(self):
        return iter(self._values)

    def __getitem__(self, index):
        return float(self._values[index])

    def __setitem__(self, index, value):
        self._values[index] = float(value)

    def __repr__(self):
        return "(%s, %s, %s)" % (str(self._values[0]), str(self._values[1]), str(self._values[2]), str(self._values[3]))

    def __mul__(self, other):
        """ Only scalar multiplication is allowed -- other must be scalar."""
        return Color(self._values[0] * other, self._values[1] * other, self._values[2] * other, self._values[3])

    __rmul__ = __mul__

    def __div__(self, other):
        """ Only scalar division is allowed -- other must be scalar."""
        # If other is not scalar then an exception will be thrown because
        # __rdiv__ is not defined for vector
        return Color(self._values[0] / other, self._values[1] / other, self._values[2] / other, self._values[3])

    @property
    def red(self):
        return self._values[0]
    @red.setter
    def red(self, value):
        self._values[0] = float(value)
    
    @property
    def green(self):
        return self._values[1]
    @green.setter
    def green(self, value):
        self._values[1] = float(value)
    
    @property
    def blue(self):
        return self._values[2]
    @blue.setter
    def blue(self, value):
        self._values[2] = float(value)
    
    @property
    def alpha(self):
        return self._values[3]
    @alpha.setter
    def alpha(self, value):
        self._values[3] = float(value)
    
    @classmethod
    def fromHSV(cls, h, s, v):
        return cls(*colorsys.hsv_to_rgb(h, s, v))
    
    @classmethod
    def gray(cls, luminance):
        return cls(luminance,luminance,luminance,1)

    def getHSV(self):
        return colorsys.rgb_to_hsv(self._values[0:3])

black = Color(0,0,0,1)
white = Color(1,1,1,1)

red = Color(1,0,0,1)
green = Color(0,1,0,1)
blue = Color(0,0,1,1)

yellow = Color(1,1,0,1)
cyan = Color(0,1,1,1)
magenta = Color(1,0,1,1)

orange = Color(1,0.6,0,1)
